/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alcatelparser.modules.io;

import alcatelparser.modules.ApplicationSettings;
import alcatelparser.modules.Env;
import alcatelparser.modules.I;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 *
 * @author geniuz
 */
public class FileWriter {
    public FileWriter(String outfilepath) throws FileNotFoundException
    {
        _overallFileBytesWrote = 0L;
        _overallFileRecordsWrote = 0L;
        _currentLimitBytesWrote = 0L;
        _currentLimitRecordsWrote = 0L;
        _currentRecordNumber = 1L;
        _fNamer = new FileNamer();
        //_tPrinter = new TraceWindowPrinter();
        //String s = _fNamer.generateFileName();
        createFile(new File(outfilepath));
    }

    public final void write(String s) throws IOException
    {
        if((new RecordsFilter()).isInFilter(_currentRecordNumber))
        {
            _overallFileBytesWrote += s.length();
            _currentLimitBytesWrote += s.length();
            //_tPrinter.write(s);
            _fos.write(s.getBytes());
        }
    }

    public final void writeSummary(String s) throws IOException
    {
        if(!_fos.isWroteToFile())
        {
            _fos.deleteFile();
            _fos.close();
            _fos = new FileWriterStream(_oldFile, true);
        }
        if(s != null)
        {
            //_tPrinter.write(s);
            _fos.write(s.getBytes());
            _overallFileBytesWrote += s.length();
            _currentLimitBytesWrote += s.length();
        }
    }

    public final void writeOverallHeader(String s) throws IOException
    {
        //_tPrinter.write(s);
        _fos.write(s.getBytes());
        _overallFileBytesWrote += s.length();
        _currentLimitBytesWrote += s.length();
    }

    public final void notifyRecordEnd() throws FileNotFoundException, IOException
    {
        if((new RecordsFilter()).isInFilter(_currentRecordNumber))
        {
            _overallFileRecordsWrote++;
            _currentLimitRecordsWrote++;
            if(isNeededToCreateAnotherFile())
            {
                closeOutStream();
                createFile(new File(getFullFilePath(_fNamer.generateFileName())));
            }
        }
        _currentRecordNumber++;
    }

    private void createFile(File file) throws FileNotFoundException
    {
        _currentLimitBytesWrote = 0L;
        _currentLimitRecordsWrote = 0L;
        if(Env.getEnv().isTraceWindowParsing())
        {
            _fos = new FileWriterStream();
            return;
        }

        if(_fos != null)
            _oldFile = _fos.getFile();
        if(file.exists())
            file.delete();
        _fos = new FileWriterStream(file);
    }

    public final void close() throws IOException
    {
        //_tPrinter.populatTraceWindow();
        resetCounters();
        closeOutStream();
    }

    private void closeOutStream() throws IOException
    {
        if(_fos != null)
        {
            _fos.flush();
            _fos.close();
        }
    }

    private void resetCounters()
    {
        _overallFileBytesWrote = 0L;
        _overallFileRecordsWrote = 0L;
        _currentRecordNumber = 0L;
    }

    private boolean isNeededToCreateAnotherFile()
    {
        String s = ApplicationSettings.getInstance().getProperty(I.I(4152));
        //MaxFileSizePanel maxfilesizepanel = (MaxFileSizePanel)Env.getEnv().get(I.I(4169));
        if(I.I(4192).equals(s))
        {return false;}//return _currentLimitBytesWrote >= (long)(maxfilesizepanel.getSizeAmount() * 1024);
        if(I.I(4195).equals(s))
        {return false;}//return _currentLimitRecordsWrote >= (long)maxfilesizepanel.getRecordsAmount();
        else
            return false;
    }

    private String getFullFilePath(String s)
    {
        return (new StringBuilder()).append(ApplicationSettings.getInstance().getProperty(I.I(769))).append(File.separator).append(s).toString();
    }

    public final int getRecordNumber()
    {
        return (int)_currentRecordNumber;
    }

    public final int getWroteRecordsNumber()
    {
        return (int)_overallFileRecordsWrote;
    }

    public final long getWroteSize()
    {
        return _overallFileBytesWrote;
    }

    private long _overallFileBytesWrote;
    private long _overallFileRecordsWrote;
    private long _currentLimitBytesWrote;
    private long _currentLimitRecordsWrote;
    private FileWriterStream _fos;
    private FileNamer _fNamer;
    //private TraceWindowPrinter _tPrinter;
    private long _currentRecordNumber;
    private File _oldFile;
    //private static final Logger LOG = Logger.getLogger(com/alcatel/algo/out/outfilter/FileWriter);


}
